package cn.com.wxd.util.framework;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import cn.com.wxd.util.SpringContextHolder;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * Title:LocalCache本地缓存
 * Description:cn.com.wxd.util.framework
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-7-22 下午4:30:04
 *
 * @author WangXuDong
 * @version 1.0
 */
public class LocalCacheUtil {

    private static final Logger log = LoggerFactory.getLogger(LocalCacheUtil.class);
    private CacheManager manager;
    private static LocalCacheUtil ehCache;

    private LocalCacheUtil() {
        //从spring持有类中获取CacheManage
        EhCacheCacheManager ehc = SpringContextHolder.getBean("ehCacheCacheManager", EhCacheCacheManager.class);
        manager = ehc.getCacheManager();
        if (manager == null) {
            try {
                throw new Exception("CacheManage无法从SpringAOP中获取。");
            } catch (Exception e) {
                log.error("请在Spring中配置ehCacheManager", e);
            }
        }
    }

    /**
     * 获取EhcacheUtil实例
     *
     * @return
     */
    public static LocalCacheUtil getInstance() {
        if (ehCache == null) {
            ehCache = new LocalCacheUtil();
        }
        return ehCache;
    }

    /**
     * 添加一个缓存
     *
     * @param name 名称
     */
    public void addCache(String name) {
        try {
            manager.addCache(name);
        } catch (IllegalStateException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 获取一个缓存,如果没有则创建一个
     *
     * @param name
     *
     * @return
     */
    public Cache getCache(String name) {
        Cache cache;
        try {
            cache = manager.getCache(name);
            if (cache == null) {
                manager.addCache(name);
                cache = manager.getCache(name);
            }
        } catch (IllegalStateException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        return cache;
    }

    /**
     * 放入某个数据到缓存
     *
     * @param cacheName
     * @param key
     * @param value
     */
    public void put(String cacheName, String key, Object value) {
        Cache cache = manager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }

    /**
     * 从某个缓存拿数据
     *
     * @param cacheName
     * @param key
     *
     * @return
     */
    public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null ? null : element.getObjectValue();
    }

    /**
     * 清理某个缓存下的某个数据
     *
     * @param cacheName
     * @param key
     */
    public void remove(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
    }

    /**
     * 清空某个缓存的所有数据
     *
     * @param cacheName
     */
    public void removeAll(String cacheName) {
        Cache cache;
        try {
            cache = manager.getCache(cacheName);
        } catch (IllegalStateException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        if (cache != null) {
            cache.removeAll();
        }
    }

    /**
     * 将某个缓存存入磁盘中
     *
     * @param cacheName
     */
    public void flush(String cacheName) {
        Cache cache;
        try {
            cache = manager.getCache(cacheName);
        } catch (IllegalStateException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        if (cache != null) {
            cache.flush();
        }
    }
}
